﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class StoreManager : PageBase
{
    private string _inStockID
    {
        get
        {
            if (ViewState["InStockID"] == null)
            {
                ViewState["InStockID"] = "";
            }
            return Convert.ToString(ViewState["InStockID"]);
        }
        set { ViewState["InStockID"] = value; }
    }

    private string _action
    {
        get 
        {
            if (ViewState["Action"] == null)
            {
                ViewState["Action"] = "";
            }
            return Convert.ToString(ViewState["Action"]);
        }
        set { ViewState["Action"] = value; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "InStock_Date";
            ViewState["OrderDire"] = "Asc";

            DataBandStockList();
            DataBandAreaList(this.ddlStock.SelectedValue);
            DataBandFloorList(this.ddlArea.SelectedValue);
            DataBandStockTypeList();

            BindGridView();
        }

    }

    protected void BindGridView()
    {
        StoreViewBLL storeViewBLL = new StoreViewBLL();
        if (this.BarCode.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("BAR_CODE", this.BarCode.Text.Trim());
        }
        if (this.ProductName.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("ProduceName", this.ProductName.Text.Trim());
        }
        if (this.ProductCode.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("CODE", this.ProductCode.Text.Trim());
        }
        if (this.ClientName.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("ClientName", this.ClientName.Text.Trim());
        }
        if (this.ddlStockType.SelectedValue != "" && this.ddlStockType.SelectedValue != "-1")
        {
            storeViewBLL.AddEqualCondition("STOCK_TYPE", this.ddlStockType.SelectedValue);
        }

        storeViewBLL.AddOrderByCondition(ViewState["SortOrder"] + " " + ViewState["OrderDire"]);

        var search = storeViewBLL.GetStoreViewList();
        if (this.ddlStock.SelectedValue != "-1")
        {
            search = search.Where(t => t.STOCK_ID.ToString() == this.ddlStock.SelectedValue);
        }
        if (this.ddlArea.SelectedValue != "-1")
        {
            search = search.Where(t => t.AREA_ID.ToString() == this.ddlArea.SelectedValue);
        }
        if (this.ddlFloor.SelectedValue != "-1")
        {
            search = search.Where(t => t.FloorID.ToString() == this.ddlFloor.SelectedValue);
        }

        this.gvInStockList.DataSource = search.ToList();
        this.gvInStockList.DataBind();
    }

    protected void gvInStockList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvInStockList.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    //protected void gvInStockList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    string ID = gvInStockList.DataKeys[e.RowIndex]["ID"].ToString();
    //    if (new InStockBLL().IsAudited(ID))
    //    {
    //        ShowMessage("审核通过的不能删除");
    //    }
    //    else {
    //        Response.Redirect("InStockManager.aspx?Action=del&ID="+ID);
    //    }
    //}
    //protected void gvInStockList_RowEditing(object sender, GridViewEditEventArgs e)
    //{
    //    string ID = gvInStockList.DataKeys[e.NewEditIndex]["ID"].ToString();
    //    if (new InStockBLL().IsAudited(ID))
    //    {
    //        ShowMessage("审核通过的不能编辑");
    //    }
    //    else
    //    {
    //        Response.Redirect("InStockDetail.aspx?Action=edit&ID=" + ID);
    //    }
    //}

    protected void DataBandStockTypeList()
    {
        CommomMethod.BindEnumForDropdwonList(this.ddlStockType, typeof(Enumerations.StockType));
        this.ddlStockType.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandStockList()
    {
        this.ddlStock.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        this.ddlStock.DataSource = storeHouseBLL.GetStockList();
        this.ddlStock.DataBind();
        this.ddlStock.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandAreaList(string StockID)
    {
        this.ddlArea.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        if (StockID != "-1")
        {
            this.ddlArea.DataSource = storeHouseBLL.GetStockAreaList(StockID);
        }
        this.ddlArea.DataBind();
        this.ddlArea.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandFloorList(string AreaID)
    {
        this.ddlFloor.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        if (AreaID != "-1")
        {
            this.ddlFloor.DataSource = storeHouseBLL.GetFloorList(AreaID);
        }
        this.ddlFloor.DataBind();
        this.ddlFloor.Items.Insert(0, new ListItem("请选择", "-1"));
    }
    protected void ddlStock_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandAreaList(this.ddlStock.SelectedValue);
        DataBandFloorList(this.ddlArea.SelectedValue);
    }
    protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandFloorList(this.ddlArea.SelectedValue);
    }
    protected void searchButton_Click(object sender, ImageClickEventArgs e)
    {
        BindGridView();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        StoreViewBLL storeViewBLL = new StoreViewBLL();
        if (this.BarCode.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("BAR_CODE", this.BarCode.Text.Trim());
        }
        if (this.ProductName.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("ProduceName", this.ProductName.Text.Trim());
        }
        if (this.ProductCode.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("CODE", this.ProductCode.Text.Trim());
        }
        if (this.ClientName.Text.Trim() != "")
        {
            storeViewBLL.AddLikeCondition("ClientName", this.ClientName.Text.Trim());
        }
        if (this.ddlStockType.SelectedValue != "" && this.ddlStockType.SelectedValue != "-1")
        {
            storeViewBLL.AddEqualCondition("STOCK_TYPE", this.ddlStockType.SelectedValue);
        }

        storeViewBLL.AddOrderByCondition(ViewState["SortOrder"] + " " + ViewState["OrderDire"]);

        var search = storeViewBLL.GetStoreViewList();
        if (this.ddlStock.SelectedValue != "-1")
        {
            search = search.Where(t => t.STOCK_ID.ToString() == this.ddlStock.SelectedValue);
        }
        if (this.ddlArea.SelectedValue != "-1")
        {
            search = search.Where(t => t.AREA_ID.ToString() == this.ddlArea.SelectedValue);
        }
        if (this.ddlFloor.SelectedValue != "-1")
        {
            search = search.Where(t => t.FloorID.ToString() == this.ddlFloor.SelectedValue);
        }

        Session["StoreData"] = search.ToList();
        string js = "<script language='javascript'>window.open(\"../report/ReportView.aspx?type=store\",\"_blank\");</script>";
        Response.Write(js);
    }
    protected void gvInStockList_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sortExpression)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
                ViewState["OrderDire"] = "Asc";
            else
                ViewState["OrderDire"] = "Desc";
        }
        else
        {
            ViewState["SortOrder"] = sortExpression;
        }
        BindGridView();   
    }
}
